﻿using Newtonsoft.Json;
using System;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Yunwei.Core.MVC
{
    /// <summary>
    /// 使用Newtonsoft.Json进行对象序列化，返回Json格式到客户端。
    /// </summary>
    public class JsonNetResult : JsonResult
    {
        public int StatusCode = 200;

        public string StatusDescription = null;

        public JsonSerializerSettings SerializerSettings
        {
            get;
            set;
        }

        public Formatting Formatting
        {
            get;
            set;
        }

        /// <summary>
        /// 返回JSON格式数据到客户端
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static JsonNetResult Success(object data)
        {
            var data2 = new
            {
                Error = 0,
                Data = data
            };
            return new JsonNetResult(data2, 200);
        }

        /// <summary>
        /// 返回错误信息到客户端.
        /// </summary>
        /// <param name="errorMessage">错误消息</param>
        /// <param name="errorCode">错误代码</param>
        /// <returns></returns>
        public static JsonNetResult Error(string errorMessage, int statusCode = 500, int errorCode = 1)
        {
            var data = new
            {
                Error = errorCode,
                Message = errorMessage
            };

            //Log.Error("Error JsonNetResult:" + errorMessage);
            JsonNetResult jsonNetResult = new JsonNetResult();
            jsonNetResult.Data = data;
            jsonNetResult.StatusCode = statusCode;
            jsonNetResult.StatusDescription = errorMessage;
            return jsonNetResult;
        }

        /// <summary>
        /// 发生异常的时候返回JSON错误到客户端
        /// </summary>
        /// <param name="ex">异常信息</param>
        /// <param name="statusCode">返回客户端的状态码，默认500</param>
        /// <param name="errorCode">错误代码</param>
        /// <returns></returns>
        public static JsonNetResult Error(Exception ex, int statusCode = 500, int errorCode = 1)
        {
            var data = new
            {
                Error = errorCode,
                Message = ex.Message
            };

            //Log.Error("Error JsonNetResult:", ex);
            JsonNetResult jsonNetResult = new JsonNetResult();
            jsonNetResult.Data = data;
            jsonNetResult.StatusCode = statusCode;
            jsonNetResult.StatusDescription = ex.Message;
            return jsonNetResult;
        }

        public JsonNetResult()
        {
            this.SerializerSettings = new JsonSerializerSettings();
            this.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
        }

        public JsonNetResult(object data, int statusCode = 200)
        {
            this.SerializerSettings = new JsonSerializerSettings();
            this.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            base.Data = data;
            this.StatusCode = statusCode;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = ((!string.IsNullOrEmpty(base.ContentType)) ? base.ContentType : "application/json");
            if (base.ContentEncoding != null)
            {
                response.ContentEncoding = base.ContentEncoding;
            }
            else
            {
                response.ContentEncoding = Encoding.UTF8;
            }
            response.StatusCode = this.StatusCode;
            if (!string.IsNullOrEmpty(this.StatusDescription))
            {
                response.StatusDescription = HttpUtility.UrlEncode(this.StatusDescription);
            }
            if (base.Data != null)
            {
                JsonTextWriter jsonTextWriter = new JsonTextWriter(response.Output);
                jsonTextWriter.Formatting = this.Formatting;
                JsonTextWriter jsonTextWriter2 = jsonTextWriter;
                JsonSerializer jsonSerializer = JsonSerializer.Create(this.SerializerSettings);
                jsonSerializer.Serialize(jsonTextWriter2, base.Data);
                jsonTextWriter2.Flush();
            }
        }
    }
}
